package com.fingard.dsp.bank.directbank.spdb01;

import com.fingard.dsp.bank.directbank.bankInterface.IDeposit;
import com.fingard.dsp.bank.reqrespobj.deposit.Req7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.RetAts7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.detail.RetAts7507Detail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class SPDB01Deposit extends SPDB01Base implements IDeposit{
    @Override
    public RetAts7507DepQry d_7507_DepQry(Req7507DepQry p_req7507) throws Exception {
        RetAts7507DepQry retAts = new RetAts7507DepQry(p_req7507.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
            SPDB01Header bankHeader = new SPDB01Header();
            bankHeader.transCode = ("2".equals(p_req7507.savingType)?"FT20":"FN20");
            int queryNumber = 20;//查询的笔数
			int beginNumber = -1;//查询的起始笔数
			int totalCount = 0;
			while(beginNumber<=totalCount && tmpRespCode.length() == 0){//分页查询
				
				if(beginNumber < 0){
					beginNumber = 1;
				}			
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
			xmlWriter.autoFormat = false;
			xmlWriter.writeStartElement("body");
			xmlWriter.writeElementString("transMasterID", bankFront.org);//签约客户号
			xmlWriter.writeElementString("queryNumber", String.valueOf(queryNumber));
			xmlWriter.writeElementString("beginNumber", String.valueOf(beginNumber));
			xmlWriter.writeEndDocument();
			
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(),bankHeader);
            XmlTextReader tmpBkReader;
			
			if(tmpStrRet[0].length() > 0){//判断通讯是否失败
				tmpRespCode = "-1";
				tmpRespInfo = tmpStrRet[1];
				break;
			}
			StringBuilder tmpSbMsg = new StringBuilder();
			tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
			if(tmpBkReader == null){//判断加载xml是否成功
				tmpRespCode = "-1";
				tmpRespInfo = tmpSbMsg.toString();
				break;
			}
			int tmpRdCount = 0;
			while(tmpBkReader.read("/packet/body/lists/list")){
				tmpRdCount++;
                XElement readXElement=tmpBkReader.readedXElement;
                RetAts7507Detail retAts7507Detail = new RetAts7507Detail();
                retAts7507Detail.actNum=p_req7507.actNum;//银行账号
                retAts7507Detail.actArea=p_req7507.actArea;//账号地区区域码
                retAts7507Detail.depositCode=readXElement.getSingleTextNotNull("acctNo").trim();//定存单号
                retAts7507Detail.startDate=readXElement.getSingleTextNotNull("accountOpenDate").trim();//起始日期
                retAts7507Detail.endDate=readXElement.getSingleTextNotNull("maturityDate").trim();//截止日期
                retAts7507Detail.valueDate=readXElement.getSingleTextNotNull("interestDate").trim();//起息日期
                String terms=readXElement.getSingleTextNotNull("type").trim();//期限
                if(p_req7507.savingType.equals("2")) {
                retAts7507Detail.terms=getBankConfig().getMapValueByKey("termMapping",terms);//存款期限 (七天`1,一个月`2,三个月`3,六个月`4,一年`5,二年`6,三年`7,五年`8,其它`9)
                }else {
                    retAts7507Detail.terms=getBankConfig().getMapValueByKey("termMappings",terms);//1-一天通知 7-七天通知 A-利多多A
                }
                retAts7507Detail.currencyID=convertCurToATS(readXElement.getSingleTextNotNull("currency"));//币种
                retAts7507Detail.noteMoney=readXElement.getSingleTextNotNull("balance").trim();//金额
                retAts7507Detail.rate=readXElement.getSingleTextNotNull("rate").trim().replace("%","");//利率(%)
                retAts7507Detail.interestMoney=readXElement.getSingleTextNotNull("totalInterest").trim();//利息
                retAts7507Detail.depositStates=readXElement.getSingleTextNotNull("status").trim();//0-正常 1-销户
                String dumpFlag=readXElement.getSingleTextNotNull("depositWay").trim();//0 :不续存/柜台取息  1-存本转息/息转活期 2-本息续存
                retAts7507Detail.dumpFlag=dumpFlag;
                retAts7507Detail.drawDate="";//上划日期
                retAts.writeRetDetail(retAts7507Detail);
            } 
			beginNumber += tmpRdCount;
			if(tmpRdCount == 0){
				break;
			}  
            //模拟返回
            //WriteBankLogStep2(xmlWriter.toXmlString());
            //String[] tmpStrRet=new String[2];
            //tmpStrRet[0]="";
            //tmpStrRet[1]=FileHelper.readFileText("D:\\zjbr-开发便捷小工具类\\xmlDealWith\\cgn02Test.txt", "utf-8");
            //WriteBankLogStep3(tmpStrRet[1]);
           
        }
      } catch (Exception e) {
          tmpRespCode = "-1";
          tmpRespInfo = e.getMessage();
          e.printStackTrace();
      } 
        if ("-1".equals(tmpRespCode) && !retAts.hasWrittenRespCode) {
            retAts = new RetAts7507DepQry(p_req7507.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("查无存单");
        }
        retAts.writeEnd();
        return retAts;
    }
            
  }

